{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tkinter\n",
    "import tkinter.messagebox\n",
    "import tkinter.ttk\n",
    "\n",
    "# 创建 tkinter 应用程序\n",
    "root = tkinter.Tk()\n",
    "# 设置窗口标题\n",
    "root.title('Select window')\n",
    "# 定义窗口大小\n",
    "root['height'] = 400\n",
    "root['width'] = 320\n",
    "\n",
    "# 与姓名关联的变量\n",
    "varName = tkinter.StringVar()\n",
    "varName.set('')\n",
    "\n",
    "labelName = tkinter.Label(root,\n",
    "                          text='Name:',\n",
    "                          justify=tkinter.RIGHT,\n",
    "                          width=50)\n",
    "labelName.place(x=10, y=5, width=50, height=20)\n",
    "\n",
    "entryName = tkinter.Entry(root,\n",
    "                          width=120,\n",
    "                          textvariable=varName)\n",
    "entryName.place(x=70, y=5, width=120, height=20)\n",
    "\n",
    "labelGrade = tkinter.Label(root,\n",
    "                           text='Grade:',\n",
    "                           justify=tkinter.RIGHT,\n",
    "                           width=50)\n",
    "labelGrade.place(x=10, y=40, width=50, height=20)\n",
    "\n",
    "# 模拟学生所在年级，字典键为年级，字典值为班级\n",
    "studentClasses = {'1': ['1', '2', '3', '4'],\n",
    "                  '2': ['1', '2'],\n",
    "                  '3': ['1', '2', '3']}\n",
    "# 学生年级组合框\n",
    "comboGrade = tkinter.ttk.Combobox(root, width=50,\n",
    "                                  values=tuple(studentClasses.keys()))\n",
    "comboGrade.place(x=70, y=40, width=50, height=20)\n",
    "\n",
    "# 事件处理函数\n",
    "def comboChange(event):\n",
    "    grade = comboGrade.get()\n",
    "    if grade:\n",
    "        # 动态改变组合框可选项\n",
    "        comboClass['values'] = studentClasses.get(grade)\n",
    "    else:\n",
    "        comboClass.set([])\n",
    "\n",
    "comboGrade.bind('<<ComboboxSelected>>', comboChange)\n",
    "\n",
    "labelClass = tkinter.Label(root,\n",
    "                           text='Class:',\n",
    "                           justify=tkinter.RIGHT,\n",
    "                           width=50)\n",
    "labelClass.place(x=130, y=40, width=50, height=20)\n",
    "\n",
    "# 学生班级组合框\n",
    "comboClass = tkinter.ttk.Combobox(root, width=50)\n",
    "comboClass.place(x=180, y=40, width=50, height=20)\n",
    "\n",
    "labelSex = tkinter.Label(root,\n",
    "                         text='Sex:',\n",
    "                         justify=tkinter.RIGHT,\n",
    "                         width=50)\n",
    "labelSex.place(x=10, y=70, width=50, height=20)\n",
    "\n",
    "# 与性别关联的变量，1:男; 0:女，默认为男\n",
    "sex = tkinter.IntVar()\n",
    "sex.set(1)\n",
    "\n",
    "# 单选钮，男\n",
    "radioMan = tkinter.Radiobutton(root,\n",
    "                               variable=sex,\n",
    "                               value=1,\n",
    "                               text='Man')\n",
    "radioMan.place(x=70, y=70, width=70, height=20)\n",
    "\n",
    "# 单选钮，女\n",
    "radioWoman = tkinter.Radiobutton(root,\n",
    "                                 variable=sex,\n",
    "                                 value=0,\n",
    "                                 text='Woman')\n",
    "radioWoman.place(x=130, y=70, width=70, height=20)\n",
    "\n",
    "# 与是否班长关联的变量，默认当前学生不是班长\n",
    "monitor = tkinter.IntVar()\n",
    "monitor.set(0)\n",
    "\n",
    "# 复选框，选中时变值为1，未选中时变值为0\n",
    "checkMonitor = tkinter.Checkbutton(root,\n",
    "                                   variable=monitor,\n",
    "                                   onvalue=1,\n",
    "                                   offvalue=0,\n",
    "                                   text='Is Monitor?')\n",
    "checkMonitor.place(x=20, y=100, width=100, height=20)\n",
    "\n",
    "# 添加按钮单击事件处理函数\n",
    "def addInformation():\n",
    "    result = 'Name:' + entryName.get()\n",
    "    result = result + '\\nGrade:' + comboGrade.get()\n",
    "    result = result + '\\nClass:' + comboClass.get()\n",
    "    result = result + '\\nSex:\\n' \\\n",
    "                   + ('Man' if sex.get() else 'Woman')\n",
    "    result = result + '\\nMonitor:\\n' \\\n",
    "                      + ('Yes' if monitor.get() else 'No')\n",
    "    # 把信息插入到列表框组件中\n",
    "    listBoxStudents.insert(0, result)\n",
    "\n",
    "buttonAdd = tkinter.Button(root,\n",
    "                           text='Add',\n",
    "                           width=40,\n",
    "                           command=addInformation)\n",
    "buttonAdd.place(x=130, y=100, width=40, height=20)\n",
    "\n",
    "# 删除按钮的事件处理函数\n",
    "def deleteSelection():\n",
    "    selection = listBoxStudents.curselection()\n",
    "    if not selection:\n",
    "        tkinter.messagebox.showinfo(title='Information',\n",
    "                                    message='No Selection!')\n",
    "    else:\n",
    "        listBoxStudents.delete(selection)\n",
    "\n",
    "buttonDelete = tkinter.Button(root,\n",
    "                           text='DeleteSelection',\n",
    "                           width=100,\n",
    "                           command=deleteSelection)\n",
    "buttonDelete.place(x=180, y=100, width=100, height=20)\n",
    "\n",
    "#创建列表框组件\n",
    "listBoxStudents = tkinter.Listbox(root, width=300)\n",
    "listBoxStudents.place(x=10, y=130, width=300, height=200)\n",
    "\n",
    "#启动消息循环\n",
    "root.mainloop()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
